package com.cloud.poetry.queue.controller;

import com.cloud.poetry.base.entity.param.QueueParam;
import com.cloud.poetry.queue.producer.KafkaProducer;
import com.cloud.poetry.queue.producer.RabbitMqProducer;
import com.cloud.poetry.core.constant.QueueConstant;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * @author mengxj
 */
@Component
@AllArgsConstructor
@RequestMapping("queue")
public class QueueController {

    private KafkaProducer kafkaProducer;

    private RabbitMqProducer rabbitMqProducer;

    @PostMapping("push")
    public void push(@RequestBody QueueParam param){
        String queue = param.getQueue();
        if(QueueConstant.Type.KAFKA.equals(param.getQueueType())){
            this.kafkaProducer.push(queue, param.getJson());
        }else if(QueueConstant.Type.RABBITMQ.equals(param.getQueueType())){
            this.rabbitMqProducer.pushObject(queue, param.getRoutingKey(), param.getJson());
        }else{
            throw new RuntimeException("push error! param queue type is illegal");
        }
    }
}
